package com.lebron.kotlin.ui.fragment

import android.os.Bundle
import android.support.v4.content.ContextCompat
import android.support.v7.widget.SearchView
import android.util.TypedValue
import com.lebron.kotlin.R
import com.lebron.kotlin.ui.activity.MainActivity
import com.lebron.kotlin.ui.adapter.MovieViewPagerAdapter
import com.lebron.kotlin.ui.present.impl.MoviePresentImpl
import com.lebron.kotlin.ui.view.MovieView
import com.lebron.kotlin.utils.LogUtils
import com.lebron.kotlin.utils.ToastUtil
import kotlinx.android.synthetic.main.fragment_movie.*

/**
 * @Author lebron
 * @Date 18-7-30-下午3:33
 */
class MovieFragment : BaseFragment(), MovieView, SearchView.OnQueryTextListener {
    override fun onError(error: String) {
    }


    private var present: MoviePresentImpl? = null
    private var fragments: ArrayList<BaseFragment> = ArrayList()
    /**
     * 搜索数据提交
     */
    override fun onQueryTextSubmit(query: String?): Boolean {
        ToastUtil.toast(query.orEmpty())
        return true
    }

    override fun onQueryTextChange(newText: String?): Boolean {

        return true
    }

    private var mActivity: MainActivity? = null
    private var searchView: SearchView? = null


    override fun getLayoutId(): Int {
        return R.layout.fragment_movie
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setHasOptionsMenu(true)//加上这句话，menu才会显示出来
        LogUtils.debug(TAG, "onCreate")
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        mActivity = activity as MainActivity
        mActivity?.setTool(toolbar)
    }

    override fun initData() {
    }

    override fun initView() {
        present = MoviePresentImpl()
        present?.attachView(this)
        immersionBar?.statusBarView(R.id.view)?.init()
        initToolbar()

        initTabBar()
    }

    private fun initToolbar() {
        toolbar_title.text = getString(R.string.string_movie)
        toolbar.inflateMenu(R.menu.toolbar_menu_search)
        searchView = toolbar.menu.getItem(0).actionView.findViewById(R.id.menu_item_search)
        searchView?.isSubmitButtonEnabled = false//显示提交按钮
        //设置最大宽度
//        searchView?.maxWidth = 500
        //设置输入框提示语
//        searchView?.queryHint = "搜索电影相关信息"
        val mSearchAutoComplete = searchView?.findViewById<SearchView.SearchAutoComplete>(R.id.search_src_text)
        mSearchAutoComplete?.setHintTextColor(ContextCompat.getColor(requireContext(), R.color.white))
        mSearchAutoComplete?.setTextColor(ContextCompat.getColor(requireContext(), R.color.white))
        mSearchAutoComplete?.setTextSize(TypedValue.COMPLEX_UNIT_PX, requireContext().resources.getDimension(R.dimen.x40))
        searchView?.setOnQueryTextListener(this)
    }

    private fun initTabBar() {
        fragments.add(MovieListFragment().newInstance(resources.getStringArray(R.array.movie_tab_type)[0]))
        fragments.add(MovieListFragment().newInstance(resources.getStringArray(R.array.movie_tab_type)[1]))
        fragments.add(MovieListFragment().newInstance(resources.getStringArray(R.array.movie_tab_type)[2]))
        fragments.add(MovieListFragment().newInstance(resources.getStringArray(R.array.movie_tab_type)[3]))
        viewPager.adapter = MovieViewPagerAdapter(childFragmentManager, fragments)
        viewPager.offscreenPageLimit = 4
        viewPager.currentItem = 0
        tab_layout.setupWithViewPager(viewPager)
        tab_layout.setSelectedTabIndicatorColor(ContextCompat.getColor(requireContext(), R.color.movie_tab_color))
    }


    override fun onDestroy() {
        super.onDestroy()

        present?.detachView()
    }


}

